home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / appe Windows 1.51 / windows.c < prev    next >
Encoding:
Text File  |  1994-12-24  |  7.4 KB  |  242 lines  |  [TEXT/KAHL]

  1. // File "main.c" -
  2. // This source file is Copyright Matt Slot & Slot-Machines Ltd., © 1994
  3.  
  4. #include <Processes.h>
  5. #include <Traps.h>
  6. #include "TextServices.h"
  7.  
  8. #include "main.h"
  9. #include "windows.h"
  10. #include "prefs.h"
  11.  
  12. // * ****************************************************************************** *
  13. // Global Vars
  14.  
  15. extern GlobalsRec glob;
  16.     
  17. // * ****************************************************************************** *
  18. // * ****************************************************************************** *
  19.  
  20. void DoCreateWindow() {
  21.     Point **prefLoc;
  22.     Rect bounds, prefBounds;
  23.     
  24.     // Default location & size of window
  25.     SetRect(&bounds, 60, 60, 160, 120);    
  26.     
  27.     if (prefLoc = (Point **) GetPrefs('WLoc', 2)) {
  28.         prefBounds = bounds;
  29.         OffsetRect(&prefBounds, (*prefLoc)->h - prefBounds.left,
  30.                 (*prefLoc)->v - prefBounds.top);    
  31.  
  32.         InsetRect(&prefBounds, 6, 6);
  33.         if (RectInRgn(&prefBounds, GetGrayRgn())) {
  34.             InsetRect(&prefBounds, -6, -6);
  35.             bounds = prefBounds;
  36.             }
  37.           else {
  38.             SetHandleSize((Handle) prefLoc, sizeof(**prefLoc));
  39.             **prefLoc = topLeft(bounds);
  40.             WritePrefs((Handle) prefLoc, 'WLoc', 2);
  41.             }
  42.         }
  43.       else {
  44.         prefLoc = (Point **) NewHandle(sizeof(**prefLoc));
  45.         if (! prefLoc) return;
  46.         **prefLoc = topLeft(bounds);
  47.         WritePrefs((Handle) prefLoc, 'WLoc', 2);
  48.         }
  49.     DisposeHandle((Handle) prefLoc);
  50.         
  51.     NewServiceWindow(0, &bounds, "\p", 0, 2048, (WindowPtr) -1, -1,
  52.             (ComponentInstance) kCurrentProcess, &glob.window);
  53.     
  54.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  55.     // Insert your favorite code to initialize the window
  56.  
  57.  
  58.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  59.  
  60.     HiliteWindow(glob.window, -1);
  61.     ShowHide(glob.window, -1);
  62.     DoRevealWindow(-1);
  63.     }
  64.  
  65. // * ****************************************************************************** *
  66. // * ****************************************************************************** *
  67.  
  68. void DoDisposeWindow() {
  69.     Point **prefLoc;
  70.     GrafPtr savePort;
  71.     
  72.     ShowHide(glob.window, 0);
  73.     
  74.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  75.     // Insert your dispose code here
  76.  
  77.  
  78.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  79.     
  80.     // Just save the current window position
  81.     if (prefLoc = (Point **) NewHandle(sizeof(**prefLoc))) {
  82.         HLock((Handle) prefLoc);
  83.         GetPort(&savePort);
  84.         SetPort(glob.window);
  85.         **prefLoc = topLeft(glob.window->portRect);
  86.         LocalToGlobal(*prefLoc);
  87.         SetPort(savePort);
  88.  
  89.         WritePrefs((Handle) prefLoc, 'WLoc', 2);
  90.         DisposeHandle((Handle) prefLoc);
  91.         }
  92.         
  93.     CloseServiceWindow(glob.window);
  94.     }
  95.  
  96. // * ****************************************************************************** *
  97. // * ****************************************************************************** *
  98.  
  99. // This gets called after a click in the content region of the window
  100. // Use the global event record to localize and handle the click
  101. void DoClickWindow() {
  102.     Point localPt;
  103.     GrafPtr savePort;
  104.  
  105.     GetPort(&savePort);
  106.     SetPort(glob.window);
  107.  
  108.     localPt = glob.theEvent.where;
  109.     GlobalToLocal(&localPt);
  110.  
  111.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  112.     // Here is your click handling code
  113.  
  114.     InvertRect(&glob.window->portRect);
  115.     while(StillDown());
  116.     InvertRect(&glob.window->portRect);
  117.     
  118.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  119.  
  120.     SetPort(savePort);
  121.     }
  122.  
  123. // * ****************************************************************************** *
  124. // * ****************************************************************************** *
  125.  
  126. // This gets called after a keydown while the window is on screen
  127. // Use the global event record to localize and handle the event.
  128. // NOTE: It is not recommended to intercept general keydowns!
  129. short DoKeydownWindow() {
  130.     char theKey, theChar;
  131.     
  132.     theChar = glob.theEvent.message & charCodeMask;
  133.     theKey = (glob.theEvent.message & keyCodeMask) >> 8;
  134.     
  135.     if ((glob.theEvent.modifiers & cmdKey) && (theKey == 0x35)) {
  136.         glob.hidden = (glob.hidden) ? 0 : -1;
  137.         DoCheckVisibility();
  138.         return(0);    // Pass it thru to other floaters!
  139.         }
  140.     
  141.     return(0);        // Return TRUE to say you have intercepted the Key Event
  142.     }
  143.  
  144. // * ****************************************************************************** *
  145. // * ****************************************************************************** *
  146.  
  147. // This is your chance to make it zoom-able
  148. void DoZoomWindow(short zoomPart) {
  149.     long ticks;
  150.     GrafPtr savePort;
  151.  
  152.     GetPort(&savePort);
  153.     SetPort(glob.window);
  154.  
  155.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  156.     // Do your Zoom Stuff
  157.     
  158.     InvertRect(&glob.window->portRect);
  159.     Delay(7, &ticks);
  160.     InvertRect(&glob.window->portRect);
  161.     
  162.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  163.  
  164.     SetPort(savePort);
  165.     }
  166.  
  167. // * ****************************************************************************** *
  168. // * ****************************************************************************** *
  169.  
  170. // Simple Window update routine, just be sure to follow standard updateEvt etiquette
  171. void DoUpdateWindow() {
  172.     GrafPtr savePort;
  173.  
  174.     GetPort(&savePort);
  175.     SetPort(glob.window);
  176.     BeginUpdate(glob.window);
  177.     
  178.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  179.     // General window drawing code
  180.     
  181.     MoveTo(glob.window->portRect.left + 2, glob.window->portRect.top + 2);
  182.     LineTo(glob.window->portRect.right - 3, glob.window->portRect.bottom - 3);
  183.     MoveTo(glob.window->portRect.right - 3, glob.window->portRect.top + 2);
  184.     LineTo(glob.window->portRect.left + 2, glob.window->portRect.bottom - 3);
  185.     LineTo(glob.window->portRect.right - 3, glob.window->portRect.bottom - 3);
  186.     LineTo(glob.window->portRect.right - 3, glob.window->portRect.top + 2);
  187.     LineTo(glob.window->portRect.left + 2, glob.window->portRect.top + 2);
  188.     LineTo(glob.window->portRect.left + 2, glob.window->portRect.bottom - 3);
  189.  
  190.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  191.     
  192.     EndUpdate(glob.window);
  193.     SetPort(savePort);
  194.     }
  195.  
  196. // * ****************************************************************************** *
  197. // * ****************************************************************************** *
  198.  
  199. // Indicates when the window is being shown or hidden
  200. void DoRevealWindow(short reveal) {
  201.     
  202.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  203.     // This function is yours if you need it
  204.     
  205.  
  206.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  207.  
  208.     }
  209.  
  210. // * ****************************************************************************** *
  211. // * ****************************************************************************** *
  212.  
  213. // Simple way to earn non-interrupt time
  214. void DoIdleWindow() {
  215.  
  216.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  217.     // This function is yours if you need it
  218.     
  219.  
  220.     // ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• ••••• •••••
  221.  
  222.     }
  223.  
  224. // * ****************************************************************************** *
  225. // * ****************************************************************************** *
  226.  
  227. pascal WindowPtr SmartNewWindow(Ptr wStorage, Rect *bounds, StringPtr title, short visFlag,
  228.         short wDefProcID, WindowPtr behind, short goAwayFlag, long refcon) {
  229.     long response;
  230.     WindowPtr theWindow;
  231.     NewWindowProc newWinProc;
  232.     
  233.     newWinProc = (void *) glob.saveNewWindow;
  234.     
  235.     theWindow = (glob.hasColorQD) ? (NewCWindow(wStorage, bounds, title, visFlag,
  236.             wDefProcID, behind, goAwayFlag, refcon)) : ((*newWinProc)(wStorage, bounds,
  237.             title, visFlag, wDefProcID, behind, goAwayFlag, refcon));        
  238.     
  239.     return(theWindow);
  240.     }
  241.  
  242.